package com.open.api.util;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.open.api.bo.MessageBO;
import com.open.api.ftp.util.FileIOUtil;
import com.open.api.model.ResultModel;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class SignData {

    //public ResultModel privateSign(String data,String PRIVATE_KEY,String CHAR_SET,String SIGN_TYPE,String PUBLIC_KEY){
    public ResultModel privateSign(Map<String,String> paramMap,String PRIVATE_KEY,String CHAR_SET,String SIGN_TYPE,String PUBLIC_KEY){
        ResultModel resultModel=new ResultModel();

        //创建参数
       // Map<String, String> paramMap = new HashMap<String, String>();
       // paramMap.put("params", data);

        //拼接待加签的字符串
        String content = AlipaySignature.getSignContent(paramMap);

        //私钥-对数据加签
        String sign = null;
        try {
            sign = AlipaySignature.rsaSign(content, PRIVATE_KEY, CHAR_SET, SIGN_TYPE);
        } catch (AlipayApiException e) {
            System.out.println("私钥-对数据加签异常："+e.getMessage());
            resultModel.setSuccess(false);
            resultModel.setErrorCode("4");
            resultModel.setErrorMsg(e.getErrMsg());
            return  resultModel;
        }

        //添加签名到参数
        paramMap.put("sign", sign);


        System.out.println("签名类型 : " + SIGN_TYPE + " 编码规则: " + CHAR_SET);
        System.out.println("拼接待加签的字符串 : " + content);
        System.out.println("当前签名 : " + sign);

        if(!PUBLIC_KEY.isEmpty()){
            //公钥-对数据验签
            boolean result = false;
            try {
                result = AlipaySignature.rsaCheckV1(paramMap, PUBLIC_KEY, CHAR_SET, SIGN_TYPE);
            } catch (AlipayApiException e) {
                System.out.println("公钥-对数据验签异常："+e.getMessage());
                resultModel.setSuccess(false);
                resultModel.setErrorCode("4");
                resultModel.setErrorMsg(e.getErrMsg());
                return  resultModel;
            }

            System.out.println("验签结果 : " + result);
        }

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("签名类型",SIGN_TYPE);
        retMap.put("编码规则",CHAR_SET);
        retMap.put("当前签名",sign);

        resultModel.setSuccess(true);
        resultModel.setData(retMap);
        return resultModel;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, AlipayApiException, IOException {

        String privateKey0="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrPavt8mZXeffMjfD3SCJ7uhwdCpm9JWogaMLu8nqzD/EGdzen8BAhSt2woxcMLYdib4Fha/dYhRco1bMX+N3nVBSHgX6EWj3DIjpPNYeBwKC2OHwBjZ7VxEBT+qZnRHQKyWbN4O0NyRnKKY5a+wk1Dt+RvhcFPpmeB/IC4ntXpv38DHCbVa8yO3CaiBBEsy20GPcK/G0pb1Xj+c5aB/BUOD4Y6ekkC5TxeknodkeW6LUdGRRnCqISQC5j6MCiDu6yeoKaSfE/D2lLNcrgoOJweTP68/tduiOGkUK5J2iztNnxmdpfBrAyjs/9B0IU4RnEaxVwSWtiq/HaFoXUepfLAgMBAAECggEBAKr6HNyslvro0T7wypRc9fFL1h45I1/0J47MizZB7bk0RZgLGQsZa0GSmt9vN+f03aSvaSN4xbMg0Ar5tKRO3GHJVeYmqchEwj4/wrCr5ezsBItswoRafyk5lnFDZzrc4nCSv4748jg9fv6Xl4fH911+yEOg5SNbac6caEvxDu5oLjHYodo95GqPqBF3YiAc39yFhCXJNRcESIVOjFyEDwRlKh4LsHGijZjPOa3YlelFiX59NgM/zDqjzrllcGOIkowF4YluDjXChH3UPkyqHqxKppLHl7DUQQicUYLSXr9reeZZnpZ9k4vC0NbZxn3AnnbJSCvfdeb3kAG6stkZ/8ECgYEA5xzsH3Kk4fFIKRS6Jq1TvCDX3A8tgzew6o8QoMe7cbd+oyLOLSVZqyI82cXlpCVooUT8Xk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pJAuU8XpJ6HZHlui1HRkUZwqiEkAuY+jAog7usnqCmknxPw9pSzXK4KDicHkz+vP7XbojhpFCuSdos7TZ8ZnaXwawMo7P/QdCFOEZxGsVcElrYqvx2haF1HqXywIDAQAB";
        System.out.println(privateKey0.length());
        System.out.println(publicKey0.length());

        FileIOUtil fileIOUtil =new FileIOUtil();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String content="中电科2";

        MessageBO body=new MessageBO();
        body.setSystem("epms");
        body.setResource("place");
        body.setAction("DELETE");
        body.setData(content);
        //body.setActionurl("/place/csxx/delete/{id}");
        body.setActiondesc("场所基本信息备案_场所信息删除");
       // body.setMd5(MD5Util.MD5Encode(content,"utf8"));
        //body.setFilename(sdf.format(new Date())+"_"+System.currentTimeMillis());
     //   body.setFilename(fileIOUtil.getFileName());
        System.out.println(body.toString());
        System.out.println(JSON.toJSONString(body));

        SignData signData=new SignData();
        String data="epms_place_DELETE_2020-05-16_158961059455355832569.json";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("app_id","c52debe0-a153-11ea-2f3a-3ab8cb4b8fe2");
        paramMap.put("method","open.api.messageservice.SendMessage");
        //paramMap.put("method","open.api.messageservice.ReadFile");
        //paramMap.put("method","open.api.messageservice.DeleteTempFile");
       // paramMap.put("method","open.api.messageservice.GetMessageFromFtp");
        paramMap.put("version","1.0.0");
        paramMap.put("charset","UTF-8");
        paramMap.put("api_request_id","1");
       // String cont="epms_place_DELETE_2020-05-29_159074646971643134699.json";
        paramMap.put("content",JSON.toJSONString(body));
       // paramMap.put("content",data);
        ResultModel resultModel=signData.privateSign(paramMap,privateKey0,"UTF-8","RSA2",publicKey0);
        System.out.println(resultModel);
/*
        //公钥-加密
        String rsaEncrypt = AlipaySignature.rsaEncrypt(content, "6666", "UTF-8");
        //私钥-解密
        String rsaDecrypt = AlipaySignature.rsaDecrypt(rsaEncrypt, "6666", "UTF-8");

        System.out.println("加密前长度 ：" + content.length());
        System.out.println("加密后 ：" + rsaEncrypt);
        System.out.println("解密后 ：" + rsaDecrypt);
        System.out.println("解密后长度 ：" + rsaDecrypt.length());*/
/*
        String s=RSAUtil.testEncrypt(privateKey0,"123456");
        System.out.println(s);
        String s1=RSAUtil.testDecrypt(publicKey0,s);
        System.out.println(s1);*/

    }
}
